//
//  ShowBigImage.m
//  XiangMuB
//
//  Created by lanou3g on 16/8/19.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "ShowBigImage.h"

static CGRect oldframe;
static CGRect originalFrame;

@implementation ShowBigImage

+ (void)showBigImage:(UIImageView *)selectedImageView{
    
    UIImage *images = selectedImageView.image;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    backView.tag = 100;
    
    oldframe = [selectedImageView convertRect:selectedImageView.bounds toView:window];
    originalFrame = oldframe;
    
    backView.backgroundColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.7];
    
    backView.alpha = 0;
    UIImageView *imageViews = [[UIImageView alloc] initWithFrame:oldframe];
    
    imageViews.layer.masksToBounds = YES;
    imageViews.layer.cornerRadius = 3.0f;
    
    imageViews.image =images;
    imageViews.tag = 1;
    
    [backView addSubview:imageViews];
    [window addSubview:backView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImage:)];
    [backView addGestureRecognizer:tap];
    
    imageViews.userInteractionEnabled = YES;
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [imageViews addGestureRecognizer:pinch];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [imageViews addGestureRecognizer:pan];
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    
    [UIView animateWithDuration:0.3 animations:^{
       
        imageViews.frame = CGRectMake(0, (height - images.size.height * width / images.size.width)/2, width, images.size.height * width/images.size.width);
        
        backView.alpha = 1;
        originalFrame = oldframe = imageViews.frame;
        
    }];
    
}

+ (void)hideImage:(UITapGestureRecognizer *)gesture{
    
    UIView *background = gesture.view;
    UIImageView *imageViewview = (UIImageView *)[gesture.view viewWithTag:1];
    
    [UIView animateWithDuration:0.3 animations:^{
        imageViewview.frame = originalFrame;
        background.alpha = 0;
    } completion:^(BOOL finished) {
        
        [background removeFromSuperview];
    }];
    
    
}

+ (void)pinchAction:(UIPinchGestureRecognizer *)gesture{
    UIImageView *imageViewPin = (UIImageView *)gesture.view;
    
    CGFloat width = oldframe.size.width * gesture.scale;
    CGFloat height = oldframe.size.height * gesture.scale;
    
    imageViewPin.frame = CGRectMake(CGRectGetMidX(oldframe) - width/2, CGRectGetMidY(oldframe) - height/2, width, height);
    
    if (gesture.state == UIGestureRecognizerStateEnded) {
        oldframe = imageViewPin.frame;
    }
    
    
}

+ (void)panAction:(UIPanGestureRecognizer *)gesture{
    
    UIImageView *imageViewPan = (UIImageView *)gesture.view;
    
    UIWindow *window= [UIApplication sharedApplication].keyWindow;
    UIView *backgroundWin = [window viewWithTag:100];
    
    CGPoint oldPoint = imageViewPan.center;
    CGPoint newPoint = [gesture translationInView:backgroundWin];
    
    imageViewPan.center = CGPointMake(oldPoint.x + newPoint.x, oldPoint.y + newPoint.y);
    
    [gesture setTranslation:CGPointZero inView:backgroundWin];
    oldframe = imageViewPan.frame;
}

@end
